package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class SysResultException {
    /*程序运行异常时，开始执行
    * 返回SysResult
    * JSONP调用时，让数据按照特定格式返回
    * callback，param判断*/
    @ExceptionHandler(RuntimeException.class)
    public Object fail(Exception e, HttpServletRequest request){
        String callback=request.getParameter("callback");
        if (StringUtils.hasLength(callback)){
            e.printStackTrace();
//            有值，代表着跨域请求
            return new JSONPObject(callback, SysResult.fail());
        }
        e.printStackTrace();
        return SysResult.fail();
    }
}
